package com.bocloud.controller;

import com.bocloud.model.Pagination;
import com.bocloud.model.Result;
import com.bocloud.model.User;
import com.bocloud.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/user")
public class UserController {

    Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    UserService userService;

    /**
     * 用途：查询用户表中的所有数据，并分页显示
     *
     * @param pagination
     * @return ：Result
     */
    @RequestMapping(value = "/users")
    @ResponseBody
    public Result getAll(@RequestBody Pagination pagination) {
        Integer currPage = pagination.getCurrPage();
        Integer Page_size = pagination.getPage_Size();
        try {
            return new Result(true, userService.getAllUsers(currPage, Page_size), "分页显示所有数据");
        } catch (Exception e) {
            log.error(e.getMessage());
            return null;
        }
    }

    /**
     * 用途：根据工号和用户名模糊查询，并分页
     *
     * @param pagination
     * @return ：Result
     */
    @RequestMapping(value = "/select")
    @ResponseBody
    public Result getBy(@RequestBody Pagination pagination) {
        Integer currPage = pagination.getCurrPage();
        Integer Page_size = pagination.getPage_Size();
        User user = new User();
        user.setUserName(pagination.getUserName().trim());
        user.setJobNum(pagination.getJobNum().trim());
        try {
            return new Result(true, userService.getBy(currPage, Page_size, user), "模糊查询数据");
        } catch (Exception e) {
            log.error(e.getMessage());
            return null;
        }
    }

    /**
     * 用途：获取用户表中的用户名和工号的所有数据，做新增用户的直属上级下拉框
     *
     * @param
     * @return ：Result
     */
    @RequestMapping(value = "/getall")
    @ResponseBody
    public Result selAll() {
        return new Result(true, userService.getAllSuperior(), "增加的直属上级下拉框数据");
    }

    /**
     * 用途：获取用户表中的用户名和工号的所有数据，做修改用户的直属上级下拉框
     *
     * @param
     * @return ：Result
     */
    @RequestMapping(value = "/getsup")
    @ResponseBody
    public Result selSuperior(@RequestBody User user) {
        return new Result(true, userService.getSuperior(user), "修改的直属上级下拉框数据");
    }


    /**
     * 用途：新增用户，先判断是否存在此用户名，存在则返回false，反之则执行插入
     *
     * @param user
     * @return ：Result
     */
    @RequestMapping(value = "/insert")
    @ResponseBody
    public Result insert(@RequestBody User user) {
        System.out.println(user);
        if (userService.getById(user) != null) {
            return new Result(false, "已有此用户");
        } else {
            if (userService.addUser(user)) {
                return new Result(true, "添加成功");
            } else {
                return new Result(false, "添加失败");
            }
        }
    }

    /**
     * 用途：批量删除用户，先判断是否有用户工号传进，有则执行删除，无则返回false
     *
     * @param jobNum
     * @return ：Result
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result delete(String jobNum) {
        if (jobNum == null) {
            return new Result(false, "未选中用户");
        } else {
            if (userService.delete(jobNum)) {
                return new Result(true, "删除成功");
            } else {
                return new Result(false, "删除失败");
            }
        }
    }

    /**
     * 用途：修改用户信息所需要的数据，根据用户工号查询此用户的所有数据，返回数据
     *
     * @param user
     * @return ：Result
     */
    @RequestMapping(value = "/before")
    @ResponseBody
    public Result beforeUpdate(@RequestBody User user) {
        return new Result(true, userService.getById(user), "修改数据");
    }

    /**
     * 用途：修改用户信息
     *
     * @param user
     * @return ：Result
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Result update(@RequestBody User user) {
        if (userService.update(user)) {
            return new Result(true, "修改成功");
        } else {
            return new Result(false, "修改失败");
        }
    }


    /**
     * 用途：分配角色所需要的数据，根据用户工号，查询此用户的角色编号和角色名称
     *
     * @param user
     * @return ：Result
     */
    @RequestMapping(value = "/role")
    @ResponseBody
    public Result getRole(@RequestBody User user) {
        return new Result(true, userService.getRoleById(user), "分配角色的数据");
    }

    /**
     * 用途：分配用户角色（修改用户角色）
     *
     * @param user
     * @return ：Result
     */
    @RequestMapping(value = "/updaterole")
    @ResponseBody
    public Result updateRole(@RequestBody User user) {
        if (userService.updateRole(user)) {
            return new Result(true, "分配角色成功");
        } else {
            return new Result(false, "分配角色失败");
        }
    }

}

